<?php

namespace app\common\service\driver;

use app\common\constant\driving\ChannelConstant;
use app\common\model\driving\Channel;
use think\Db;
use think\Exception;

/**
 * 渠道服务类
 **/
class ChannelService
{

    /**
     * 是否是三方订单
     *
     * @param string $label 渠道标签
     * @return bool
     **/
    public static function is_third($label)
    {
        if (!in_array($label, [ChannelConstant::LABEL_OWN, ChannelConstant::LABEL_CREATE])) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取佣金
     *
     * @param string $label 渠道标签
     * @param float $total_fee 总金额
     * @return float
     **/
    public static function get_amount($label, $total_fee)
    {
        if ($total_fee > 0) {
            $channel = Channel::where('label', $label)
                ->where('status', ChannelConstant::STATUS_1)
                ->find();
            if ($channel['service_type'] == ChannelConstant::TYPE_1) {
                $rate = bcdiv($channel['service'], 100, 2);
                $rate = bcsub(1, $rate, 2);
                $amount = bcmul($total_fee, $rate, 2);
            } else {
                $amount = bcsub($total_fee, $channel['service'], 2);
            }
            $amount = bcsub($amount, $channel['insurance'], 2);
            $amount = bcsub($amount, $channel['commission'], 2);
            return $amount;
        }
        return $total_fee;
    }

}
